package com.qfedu.lc.server.listener;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.config.RabbitKeyConfig;
import com.qfedu.lc.common.config.RedisKeyConfig;
import com.qfedu.lc.common.param.GrowthAdd;
import com.qfedu.lc.common.param.MqMsg;
import com.qfedu.lc.server.entity.SmsLog;
import com.qfedu.lc.server.entity.UserGrowthLog;
import com.qfedu.lc.server.entity.UserLevel;
import com.qfedu.lc.server.mapper.SmsLogMapper;
import com.qfedu.lc.server.mapper.UserGrowthLogMapper;
import com.qfedu.lc.server.mapper.UserLevelMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RabbitMQListener {

    @Resource
    private StringRedisTemplate template;
    @Resource
    private SmsLogMapper dao;

    @Autowired
    private UserLevelMapper userLevelMapper;
    @Autowired
    private UserGrowthLogMapper userGrowthLogMapper;

    // 创建一个接收发送短信队列的消费
    @RabbitListener(queues = RabbitKeyConfig.Q_SENDSMS)
    public void handlerSms(MqMsg mqMsg) {
        //1.生成短信验证码
        String code = RandomUtil.randomInt(100000, 999999) + "";
        log.info("验证码，{}", code);
        log.info("msg:"+mqMsg);

        // 调用阿里云向手机发送短信
        // 记录到短信发送的表中

        //2.发送验证码
//        if (AliSmsUtil.sendCode(mqMsg.getMsg().toString(), code)) {
        //短信已验证没问题，所以为了成本 省略
        if (true) {  // 替代阿里云发送
            String key = "", type = "";
            switch (mqMsg.getType()) {
                case 1://注册
                {
                    key = RedisKeyConfig.SMS_RCODE;  // 注册用户时的redis 前缀
                    type = "注册验证码";
                }
                ;
                break;
                case 2://登录
                {
                    key = RedisKeyConfig.SMS_LCODE;
                    type = "登录验证码";
                }
                ;
                break;
                case 3://密码找回
                {
                };
                break;
            }
                //3.存储验证码 到redis
                template.opsForValue().set(key + mqMsg.getMsg(), code, RedisKeyConfig.SMS_CODE_TIME, TimeUnit.SECONDS);
                //4.记录发送信息
                dao.insert(new SmsLog(mqMsg.getMsg().toString(), type, "发送" + type + "完成！", new Date()));
//            }
        }


    }

    /**
     * 积分成长的消费者
     * @param mqMsg
     */
    @RabbitListener(queues = RabbitKeyConfig.Q_USER_GROWTH)
    public void handlerGrowth(MqMsg mqMsg) {

        System.out.println("mqMsg = " + mqMsg);
        if (mqMsg.getType()== 5){ // 积分成长的消息

            GrowthAdd growthAdd = (GrowthAdd) mqMsg.getMsg();
            Integer uid = growthAdd.getUid();

            QueryWrapper<UserLevel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",uid);

            UserLevel userLevel = userLevelMapper.selectOne(queryWrapper);
            if (userLevel==null){ // 查询不到会员等级 就是注册 插入会员信息
                userLevelMapper.insert(new UserLevel(uid,1,"练气",growthAdd.getGrowth(),new Date()));
            }else { // 已经有会员等级    登录 签到  设置头像

                // 成长的积分
                int growth = userLevel.getGrowths() + growthAdd.getGrowth();
                userLevel.setGrowths(growth);

                //根据成长以后的积分 判断用户的等级
                if (growth<1000){
                    userLevel.setLevel(1);
                    userLevel.setName("练气");
                }else if (userLevel.getGrowths() <100000) {
                    userLevel.setLevel(3);
                    userLevel.setName("金丹");
                } else if (userLevel.getGrowths() <1000000) {
                    userLevel.setLevel(4);
                    userLevel.setName("元婴");
                } else if (userLevel.getGrowths() <10000000) {
                    userLevel.setLevel(5);
                    userLevel.setName("化神");
                } else {
                    userLevel.setLevel(6);
                    userLevel.setName("仙人");
                }

                // 根据id更新积分 等级
                userLevelMapper.updateById(userLevel);
            }
            //记录更新的积分 日志
            userGrowthLogMapper.insert(new UserGrowthLog(uid,growthAdd.getType(),growthAdd.getType() +"增加积分"+growthAdd.getGrowth(),new  Date()));

        }

    }

}
